//
//  DiscoveryViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/2.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "DiscoveryViewController.h"

@interface DiscoveryViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property(strong,nonatomic)UIActivityIndicatorView *activityIndicatorView;

@property(strong,nonatomic)UIButton *leftBarButtonItem;
@end

@implementation DiscoveryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    [self titleWithLabel:@"发现" andColor:[UIColor whiteColor]];
    
    self.title = @"发现";
//    [self addBack];
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://zy.qnzyz.org.cn/prjv-exchange/exchange/exchangeList.do"]]];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.1.11:8080/prjv-exchange/exchange/exchangeList.do"]]];
//    https://zy.qnzyz.org.cn/prjv-exchange/exchange/exchangeList.do
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]];
    [self.webView setScalesPageToFit:YES];
    self.webView.delegate=self;
    self.activityIndicatorView=[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.view addSubview:self.activityIndicatorView];
    self.activityIndicatorView.center=CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    self.activityIndicatorView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"new_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickLeftBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.leftBarButtonItem=button;
    button.hidden=YES;
    self.navigationItem.leftBarButtonItems=@[leftBarButtonItem];
    
    button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 25, 25);
    [button setImage:[UIImage imageNamed:@"new_refresh"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(chickRefreshBarButtonItem:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *refreshBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.rightBarButtonItems=@[refreshBarButtonItem];
}

-(void)chickLeftBarButtonItem:(UIBarButtonItem *)sender{
    [self.webView goBack];
}

-(void)chickCloseBarButtonItem:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)chickRefreshBarButtonItem:(UIBarButtonItem *)sender{
    [self.webView reload];
}

-(void)viewDidAppear:(BOOL)animated{
    [self.view bringSubviewToFront:self.activityIndicatorView];
}

#pragma mark - webView delegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //    [SVProgressHUD show];
    
    [self.activityIndicatorView startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //    [SVProgressHUD dismiss];
    if (webView.canGoBack==YES) {
        self.leftBarButtonItem.hidden=NO;
    }else{
        self.leftBarButtonItem.hidden=YES;
    }

    [self.activityIndicatorView stopAnimating];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //    [SVProgressHUD showErrorWithStatus:@"网络错误"];
    [self.activityIndicatorView stopAnimating];
}


@end
